<template>
  <!-- 主文件调整 -->
  <div>
    <TopFilter :list="topFilterList" :cols="3" :labelWidth="110" :rows="3" :collapse="false" @filterChange="handleSearch"></TopFilter>
    <!-- 订单数据 -->
    <BreakSpace label="调整规则" :containerStyle="{ marginBottom: '10px' }"></BreakSpace>
    <FormPanel ref="purchaseForm" :list="statusFormList" :cols="4" :labelWidth="65" :isSubmitBtn="false"></FormPanel>

    <FilterTable
      ref="table"
      columnsRef="partPartInfoManagePartInfoR3PriceDownload"
      height="auto"
      maxHeight="350"
      :columns="columns"
      :fetchapi="fetchapi"
      :params="fetchParams"
      :datakey="'rows'"
      :onColumnsChange="columns => (this.columns = columns)"
      :onRowSelectChange="handleTableRowSelect"
      :onPageChange="handleTablePageChange"
    ></FilterTable>
    <div class="bottomBtnWrap">
      <el-button @click="close">取 消</el-button>
      <AjaxButton type="primary" :click="btnSave">保存</AjaxButton>
    </div>
  </div>
</template>

<script>
import moment from 'moment';
import { queryPartInfo, savePartAdjustment } from '@/api/part/partInfo/index';
import { notifyAction, messageAction, confirmAction } from '@/utils';
import { dictionary } from '@/utils/dictMixin';
import { getStorageList } from '@/api/part/common';

export default {
  mixins: [dictionary],
  data() {
    return {
      statusFormList: this.createStatusFormList(),
      topFilterList: this.createTopFilterList(),
      columns: this.createTableColumns(),
      fetchapi: queryPartInfo,
      fetchParams: { xhrAbort: true },
      selectedTableRowData: [], // 选中的table行项
      pageSize: 20 // 每页显示条数(判断当前页面是否全选)
    };
  },
  created() {
    // this.init();
  },
  methods: {
    createStatusFormList() {
      return [
        {
          type: 'SELECT',
          label: '',
          fieldName: 'a1',
          placeholder: '请选择...',
          itemList: [
            { text: '不含税销售价', value: '1' },
            { text: '不含税批发价', value: '2' }
          ]
        },
        {
          type: 'SELECT',
          label: '=',
          fieldName: 'a2',
          placeholder: '请选择...',
          itemList: [
            { text: '不含税销售价', value: '1' },
            { text: '不含税批发价', value: '2' },
            { text: '不含税采购价', value: '3' }
          ]
        },
        {
          type: 'INPUT_NUMBER',
          label: '*',
          fieldName: 'a3',
          style: { width: 'calc(100% - 40px)' },
          selfCols: 2,
          descOptions: {
            content: '%'
          }
        },

        {
          type: 'SELECT',
          label: '储备高限',
          fieldName: 'MAX_STOCK',
          placeholder: '请选择...',
          itemList: [
            { text: '是', value: 'true' },
            { text: '否', value: 'false' }
          ],
          change: val => {
            if (val === 'true') {
              this.statusFormList.find(item => item.fieldName === 'a8').disabled = false;
            } else {
              this.statusFormList.find(item => item.fieldName === 'a8').disabled = true;
            }
          }
        },
        {
          type: 'INPUT',
          label: '高限储值',
          fieldName: 'a8',
          disabled: true
        },
        {
          type: 'SELECT',
          label: '储备底限',
          fieldName: 'MIN_STOCK',
          placeholder: '请选择...',
          itemList: [
            { text: '是', value: 'true' },
            { text: '否', value: 'false' }
          ],
          change: val => {
            if (val === 'true') {
              this.statusFormList.find(item => item.fieldName === 'a9').disabled = false;
            } else {
              this.statusFormList.find(item => item.fieldName === 'a9').disabled = true;
            }
          }
        },
        {
          type: 'INPUT',
          label: '底限储值',
          fieldName: 'a9',
          disabled: true
        }
      ];
    },
    createTopFilterList() {
      return [
        {
          type: 'INPUT',
          label: '主组号',
          fieldName: 'mainGroupNo'
        },
        {
          type: 'INPUT',
          label: '配件代码',
          fieldName: 'partNo'
        },
        {
          type: 'SELECT',
          label: '配件分类代码',
          fieldName: 'partSortCode',
          placeholder: '请选择...',
          itemList: this.createDictList(8106)
        },
        {
          type: 'SELECT',
          label: '仓库',
          fieldName: 'storageCode',
          request: {
            fetchApi: getStorageList,
            valueKey: 'STORAGE_CODE',
            textKey: 'STORAGE_NAME'
          },
          change: val => {
            console.log(val);
            if (val) {
              this.topFilterList.find(x => x.fieldName == 'inOutTimeStr|inOutTimeEnd').disabled = false;
              this.topFilterList.find(x => x.fieldName == 'inOutTimeStr|inOutTimeEnd').labelOptions.disabled = false;
            }
            if (!val) {
              this.topFilterList.find(x => x.fieldName == 'inOutTimeStr|inOutTimeEnd').initialValue = '';
              this.topFilterList.find(x => x.fieldName == 'inOutTimeStr|inOutTimeEnd').disabled = true;
              this.topFilterList.find(x => x.fieldName == 'inOutTimeStr|inOutTimeEnd').labelOptions.disabled = true;
              this.topFilterList.find(x => x.fieldName == 'inOutTime').initialValue = '';
            }
          }
        },
        {
          type: 'RANGE_DATE',
          fieldName: 'inOutTimeStr|inOutTimeEnd',
          labelOptions: {
            fieldName: 'inOutTime',
            disabled: true,
            itemList: [
              { text: '入库日期', value: 'inTime' },
              { text: '出库日期', value: 'outTime' }
            ]
          },
          disabled: true
        }
      ];
    },

    createTableColumns() {
      return [
        {
          title: '配件代码',
          dataIndex: 'PART_NO',
          showOverflowTooltip: true
        },
        {
          title: '配件名称',
          width: 150,
          dataIndex: 'PART_NAME',
          showOverflowTooltip: true
        },
        {
          title: '仓库名称',
          width: 150,
          dataIndex: 'STORAGE_NAME',
          showOverflowTooltip: true
        },
        {
          title: '不含税销售价单价',
          dataIndex: 'NOT_COST_SALES_PRICE',
          showOverflowTooltip: true
        },
        {
          title: '不含税采购单价',
          dataIndex: 'PLAN_PRICE',
          showOverflowTooltip: true
        },
        {
          title: '不含税批发单价',
          dataIndex: 'COST_WHOLESALES_PRICE',
          showOverflowTooltip: true
        },
        {
          title: '储备高限',
          dataIndex: 'MAX_STOCK',
          showOverflowTooltip: true
        },
        {
          title: '储备底限',
          dataIndex: 'MIN_STOCK',
          showOverflowTooltip: true
        },

        {
          title: '状态',
          dataIndex: 'brandName',
          showOverflowTooltip: true
        }
      ];
    },

    async init() {
      const res = await queryPartInfo();
      console.log(res, 'res');
    },

    async btnSave() {
      var a1 = this.statusFormList.find(x => x.fieldName == 'a1').initialValue;
      var a2 = this.statusFormList.find(x => x.fieldName == 'a2').initialValue;
      var a3 = this.statusFormList.find(x => x.fieldName == 'a3').initialValue;
      var a8 = this.statusFormList.find(x => x.fieldName == 'a8').initialValue;
      var a9 = this.statusFormList.find(x => x.fieldName == 'a9').initialValue;
      var params = {
        a1: a1,
        a2: a2,
        a3: a3,
        a8: a8,
        a9: a9,
        rows: []
      };
      params.rows = this.selectedTableRowData;
      console.log(params);

      if (this.selectedTableRowData.length > 0) {
        var res = await savePartAdjustment(params);
        if (res.data.code == '200') {
          this.$notify.success('操作成功');

          this.fetchParams = { ...this.fetchParams };
        }
      } else {
        this.$notify.warning('请选择至少选择一条数据进行变更');
        return;
      }
    },
    // 点击搜索或者重置
    handleSearch(e) {
      console.log(e);
      if (!(!!e.storageCode || !!(e.partNo && e.partNo.length > 1))) {
        return notifyAction('请选择仓库或者至少输入两位配件代码', 'warning');
      }
      if (e.storageCode) {
        if (!e.inOutTime) {
          return notifyAction('请选择出入库类型', 'warning');
        }
        if (!e.inOutTimeStr) {
          return notifyAction('请输入时间', 'warning');
        }
        e.inOutTimeStr = e.inOutTimeStr ? e.inOutTimeStr.split(' ')[0] + ' 00:00:00' : '';
        e.inOutTimeEnd = e.inOutTimeEnd ? e.inOutTimeEnd.split(' ')[0] + ' 23:59:59' : '';
        console.log(e);
        if (e.inOutTimeStr || e.inOutTimeEnd) {
          let diffMonth = moment(e.inOutTimeEnd).diff(moment(e.inOutTimeStr), 'months');
          if (diffMonth > 11) return this.$notify.warning({ title: '提示信息', message: '起止日期跨度不能超过一年' });
        }
      }
      this.fetchParams.xhrAbort = true;
      console.log(e, '**********************');
      this.fetchParams = {
        partNo: e.partNo || '',
        mainGroupNo: e.mainGroupNo || '',
        partSortCode: e.partSortCode || '',
        inOutTime: e.inOutTime || '',
        inOutTimeStr: e.inOutTimeStr || '',
        inOutTimeEnd: e.inOutTimeEnd || '',
        storageCode: e.storageCode || '',
        xhrAbort: false
      };
    },
    // table行项选择
    handleTableRowSelect(e) {
      this.selectedTableRowData = e;
      console.log(e, '*****************');
    },
    // table分页变化
    handleTablePageChange(e) {
      this.pageSize = e.pageSize;
    },

    // 重新查询
    requeryTableData() {
      this.fetchParams.xhrAbort = true;
      this.fetchParams.xhrAbort = false;
    },
    close() {
      this.$emit('close');
    },
    // 设置查询日期初始值
    setSearchParamInitalValue() {
      let date = new Date();
      let thisMonth = date.getMonth() + 1;
      let thisYear = date.getFullYear();

      let monthFirstDay = new Date(thisYear + '/' + thisMonth + '/1');
      this.topFilterList.find(item => item.fieldName == 'adjustDate').initialValue = [monthFirstDay, new Date()];
      this.topFilterList.find(item => item.fieldName == 'outDate').initialValue = [monthFirstDay, new Date()];
    }
  }
};
</script>

<style scoped>
.bottomBtnWrap {
  position: absolute;
  left: 0;
  bottom: 0;
  right: 0;
  z-index: 9;
  border-top: 1px solid #e9e9e9;
  padding: 10px 20px;
  background: #fff;
  text-align: right;
}
</style>
